package com.comtrade.map;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.comtrade.ilserver.tasks.BeaconServer;
import com.example.beaconapk.R;

public class MapFrame extends FrameLayout {

	private Context mContext;
	private LayoutInflater layoutInflater;
	
	protected DotView dot;
	protected TouchView touchView;
	protected ArrayList<BeaconServer> beaconsList;
	public void setTouchView(TouchView touchView) {
		this.touchView = touchView;
	}

	public ArrayList<BeaconServer> getBeaconsList() {
		return beaconsList;
	}

	public void setBeaconsList(ArrayList<BeaconServer> beaconsList) {
		this.beaconsList = beaconsList;
	}

	Matrix matrix=new Matrix();
	/**
	 * Button on mapFrame
	 */
	private Button btnFindMe;

	public MapFrame(Context context) {
		super(context);
		this.mContext = context;
		inflate();
		bindViews();
	}

	private void inflate() {
		layoutInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.map_frame, this, true);
	}
	
	public void SendBeaconsToTouchView() {
		
		touchView.CreateBeaconList(beaconsList);
	}

	private void bindViews() {
		// bind all views here
		touchView = (TouchView) findViewById(R.id.touch_view);

		dot = touchView.getDot();
		dot.setCoordinates(0, 0);
		btnFindMe = (Button) findViewById(R.id.btnFindMe);
		
		btnFindMe.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Random r=new Random();
				
				//touchView.setDotCoordinates(r.nextInt(400), r.nextInt(300));
				moveViewToScreenCenter();
				
			}
		});
		
	}
	
	public DotView getDotView(){
		return dot;
	} 
	private void moveViewToScreenCenter()
	{
	    RelativeLayout root = (RelativeLayout) findViewById( R.id.rootLayout);
	    DisplayMetrics dm = new DisplayMetrics();
	   DotView dot1 = touchView.getDot();
	   //float w=touchView.getTranslatedX();
	   //float h=touchView.getTranslatedY();
	  // Log.d("Vrednosti za x y:", Float.toString(h)+"  "+ Float.toString(w));
	  // touchView.setClipBounds(new Rect(10,-10,100,100));
	  // touchView.translateCenterToDot();
	   //float tH = getMeasuredHeight()/2;
	  //float tW = getMeasuredWidth()/2;
	   
	   
	   //Log.d("VREDNOSTI", Float.toString(x)+" "+Float.toString(y)+" "+Float.toString(tH)+" "+Float.toString(tW));
	   /*matrix.postTranslate(x, y);*
		/*WindowManager windowManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(dm);
	    //mContext.getResources().getDisplayMetrics(dm);
	    //this.getWindowManager().getDefaultDisplay().getMetrics( dm );
	    int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();

	    int originalPos[] = new int[2];
	    view.getLocationOnScreen( originalPos );

	    int xDest = dm.widthPixels/2;
	    xDest -= (view.getMeasuredWidth()/2);
	    int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
<<<<<<< .mine
	    Log.d("XDest", Integer.toString(xDest));
	    Log.d("yDest", Integer.toString(yDest));
	    TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
=======
		
	    TranslateAnimation anim = new TranslateAnimation( h, x , w, y );

	    
	    anim.setDuration(1000);
	    anim.setFillAfter( true );
	    view.startAnimation(anim); */
	}
	public TouchView getTouchView(){
		return touchView;
	}
	
	public Button getButton(){
		return btnFindMe;
	}
	
	/**
	 * sets Drawable mapImage for map image with coordinates Rect(0,0,w,h);
	 * @param mapImage
	 * @param w
	 * @param h
	 */
	public void setMapImage(Drawable mapImage, int w, int h) {
		if (mapImage != null){
			touchView.setMap(mapImage, w, h);
		}
		else {
			Log.e(VIEW_LOG_TAG, "map is null");
		}
	}
}
